<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\ActivityService;
use MyLukin\IP2City\IP2City;
//use Illuminate\Http\Request;
use App\Services\CommonService;
use App\Services\MemberService;
use Session;
use Request;

class ActivityController extends Controller{
	public function list($mid){

        $activity = new ActivityService;
        // $common 		= new CommonService;
        $member = new MemberService;
        $userinfo = $member->getMemberInfoByid($mid);

        // $ip_address 	= '183.185.119.31';
        // $ipinfo 		= IP2City::ip2addr($ip_address);
        // $cityinfo 		= $common->getPcInfoByName($ipinfo[2]);
        // $provinceinfo 	= $common->getPcInfoByName($ipinfo[1]);

        $activitylist = $activity->getOngoingActivityList();
        if (!empty($activitylist)) {
            $_data = array('code' => '2x000001', 'activitylist' => $activitylist, 'userinfo' => $userinfo);
        } else {
            $_data = array('code' => '4x000001', 'msg' => '暂无活动信息');
        }

        return response()->json($_data);
	}

	public function getActivityList($cityid){
        $userinfo = Session::get('userinfo');

            $common         = new CommonService;
            $activity       = new ActivityService;

            $cityinfo       = $common->getPcInfoById($cityid);
            $provinceinfo   = $common->getPcInfoById($cityinfo->pid);
            $activitylist   = $activity->getOngoingActivityList($cityid);

            $data           = array('provinceInfo'=>$provinceinfo,'cityInfo'=>$cityinfo,'activitylist'=>$activitylist,'userinfo'=>$userinfo);
            return response()->json($data);

	}

	public function view($id,$mid){
	    $member = new MemberService;
        $userinfo = $member->getMemberInfoByid($mid);
        if(!empty($userinfo)){
            $activity = new ActivityService;
            $common = new CommonService;
            $activityinfo = $activity->getActivityInfoById($id);

            $provinceinfo = $common->getPcInfoById($activityinfo->province);
            $cityinfo = $common->getPcInfoById($activityinfo->city);
            $countyinfo = $common->getPcInfoById($activityinfo->county);

            $activityinfo->n_address = $provinceinfo->name . $cityinfo->name . $countyinfo->name . $activityinfo->address;
            $data = array('activityinfo'=>$activityinfo,'userinfo'=>$userinfo);
        }else{
            $data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }

            return response()->json($data);

	}

	public function citylist(){
		$common 		= new CommonService;
		$citylist		= $common->getCityListByLevel(2);

		$ip_address 	= '183.185.119.31';
		$ipinfo 		= IP2City::ip2addr($ip_address);

		$cityinfo 		= $common->getPcInfoByName($ipinfo[2]);

		$data 			= array('cityinfo'=>$cityinfo,'citylist'=>$citylist);
		return response()->json($data);
	}
}